package com.stm.bi.etl;

import com.stm.bi.core.util.DateUtils;
import com.stm.bi.enums.DateType;
import com.stm.framework.core.util.FileResourceLoader;
import com.stm.saas.api.PolicyDetailRemoteService;
import com.stm.saas.dto.query.PolicyDetailListQry;
import com.stm.saas.dto.vo.PolicyDetailVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liuyx
 * @date 2025/7/15  15:30
 */

@Component
public class AdsProductMultiSaleEtlTask extends AbstractDataEtlTask{
    @Resource
    private PolicyDetailRemoteService policyDetailRemoteService;



    @Override
    public boolean isTenantLine() {
        return true;
    }

    @Override
    public boolean syncWithTimeRange() {
        return true;
    }

    @Override
    public String getTaskName() {
        return "商品分层转换";
    }

    @Override
    public List<DateType> getEtlDateTypes() {
        return Arrays.asList(DateType.YEAR, DateType.MONTH, DateType.WEEK, DateType.DAY);
    }



    @Override
    public List<Map> extract(Long orgId, String orgNo, DateType dateType, LocalDateTime startDateTime, LocalDateTime endDateTime) {
        String sql = FileResourceLoader.getResourceAsString("/sql/adsProductMultiSale.sql");

        List<PolicyDetailVO> policyDetailVOS = policyDetailRemoteService.selectList(new PolicyDetailListQry()
                .setCategory("product_policy")
                .setType("product_category").setOrgId(orgId)).get();
        if(policyDetailVOS == null) {
            return new ArrayList<>();
        }
        Map<String, PolicyDetailVO> policyDetailCodeMap = policyDetailVOS.stream().collect(Collectors.toMap(PolicyDetailVO::getCode, v -> v));


        List<Map> data = new ArrayList<>();

        String date = DateUtils.getDateStrByDateType(startDateTime, dateType);
        List<Map> maps = jdbcService.selectList(sql, new Object[]{orgNo, dateType.getValue(), date, date});

        for (Map map : maps) {
            Map row = new HashMap<>();
            for (Object o : map.entrySet()) {
                Map.Entry entry = (Map.Entry) o;
                row.put(entry.getKey(), entry.getValue());
            }
            String categoryCode = (String)map.get("category_code");
            String assortmentCode = (String)map.get("assortment_code");
            if(categoryCode == null || assortmentCode == null) {
                continue;
            }
            if(!policyDetailCodeMap.containsKey(categoryCode)) {
                continue;
            }
            if(!policyDetailCodeMap.containsKey(assortmentCode)) {
                continue;
            }
            row.put("policy_id", policyDetailCodeMap.get(categoryCode).getId());
            row.put("policy_parent_id", policyDetailCodeMap.get(assortmentCode).getId());
            row.put("policy_name", policyDetailCodeMap.get(categoryCode).getName());
            row.put("policy_parent_name", policyDetailCodeMap.get(assortmentCode).getName());
            data.add(row);
        }


        return data;
    }

    @Override
    public List<Map> transform(Long orgId, String orgNo, List<Map> dataList) {
        return dataList;
    }
    @Override
    public void load(Long orgId, String orgNo, List<Map> dataList) {
        dorisLoadRepository.putLoad("ads_base_product_policy_sales",dataList);
    }

    @Override
    public int getDelayDays() {
        return 0;
    }

}
